<?php

namespace App\Console\Commands\ExpressionLanguage;

use Illuminate\Console\Command;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

class SymfonyExpressionLanguageCommand extends Command
{
    protected $signature = 'expression-language:demo';

    protected $description = 'Symfony Expression Language Demo';

    public function handle()
    {
        $language = new ExpressionLanguage();

        $cls = new class {
            public function output()
            {
                return 3;
            }
        };
        $language->register('output', function ($str) {
            return sprintf('(is_string(%1$s) ? strtolower(%1$s) : %1$s)', $str);
        }, function ($arguments, $str) {
            if (!is_string($str)) {
                return $str;
            }

            return strtolower($str);
        });
        $rule = "1 > 2";

        var_dump($language->evaluate($rule)); // displays 3

        var_dump($language->compile($rule)); // displays (1 + 2)

        return 0;
    }
}
